//数组延迟执行
Array.prototype.each4delay = function(f,t,auto,end){
	 var a = this,auto = auto == null ? true : auto ;
	 a.i = 0;
	 a.tid = 0;
	 function callback(a){
		  if( a.i < a.length ){
			 a.tid = setTimeout(function(){
				 function next(){
					callback(a);
				 };
				 f(a.i,a[a.i],next);
				 a.i ++;
				 auto && next();
			  },t|1000);
		  }else{
			 clearTimeout(a.tid);
			 end && end(a);
		  }
	 }
	 callback(a);
};
function loader(attrTpl,resources,root,props,callback){
	var attrs = attrTpl.split('.');
	if( !resources || resources.length == 0 ) return;
	resources.each4delay(function(i,content,next){
		var content = resources[i];
		if( content == "" ) return;
		var content = root + content;
		var s = document.createElement(attrs[0]);
		if( props ){
			for(var prop in props){
				var value = props[prop];
				s.setAttribute(prop,value);
				//s[prop] = props[prop];
			}
		}
		s.setAttribute(attrs[1],content);
		//s[attrs[1]] = content;
		s.onload = function(){
			next();
		};
		document.body.appendChild(s);
	},10,false,function(){
		console.info('base-loaded');
		callback && callback();
	});
}
function heredoc(fn) {
    return fn.toString().split('\n').slice(1,-1).join('\n') + '\n'
}
function loaded($){
	$.jsonp = function(url,data,success,method,callname,error,callpname){
		var post = {
			type: method||"POST",
			url: url,
			cache : false,
			async : false,
			data : data,
			dataType : "jsonp",
			jsonp : callpname||"callback",
			success: success||$.noop,
			error : error||$.noop
		};
		callname && (post.jsonpCallback=callname);
		$.ajax(post);
	}
	var template = heredoc(function(){/*
		<style>
			@import url("http://localhost:7080/css/style.css");
			@import url("http://localhost:7080/css/font-awesome.min.css");
			[down-panel]{
				background: white;
			    color: black;
			    width: 200px;
			    height: 400px;
			    position: fixed;
			    right: 100px;
			    top: 100px;
			    overflow-x: hidden;
    			overflow-y: auto;
			}
			.down-demo,.down-page{
				border: 1px solid #de686d;
			    border-radius: 4px;
			    width: 180px;
			    display: block;
			    text-align: center;
			    margin: 2px;
			    cursor:pointer;
			}
			.down-demo:hover{
				color: #fff;
    			background: #de686d;
			}
			[resources]{}
			[resource]{cursor:pointer;}
			[resource]:hover{
				background:#fbf6f6;
			}
		</style>
		<div down-panel>
			<a class="down-demo" href="javasript:void(0);">download-demo</a>
			<a class="down-page" href="javasript:void(0);">download-page</a>
			<a refresh href="javasript:void(0);">刷新资源</a>
			<ul resources></ul>
		</div>
	*/});
	$(template).appendTo('body');
	function load_resource(){
		$('[resources]').empty();
		$('link').each(function(i,elm){
			var href = $(elm).attr('href');
			var filename = href.substring(href.lastIndexOf('/')+1,href.length);
			$('[resources]').append("<li ><a href='"+href+"' resource>"+filename+"</a></li>");
		});
		$('script[src]').each(function(i,elm){
			var href = $(elm).attr('src');
			var filename = href.substring(href.lastIndexOf('/')+1,href.length);
			$('[resources]').append("<li ><a href='"+href+"' resource>"+filename+"</a></li>");
		});	
	}
	load_resource();
	window.add_resource = function(href){
		var filename = href.substring(href.lastIndexOf('/')+1,href.length);
		$('[resources]').append("<li ><a href='"+href+"' resource>"+filename+"</a></li>");
	}
	$('[refresh]').click(function(){
		load_resource();
	});
	$('.dmdown').off('click').click(function(){
		console.info('download now');
		var url = location.href;
		var service = "//localhost:8080/demo1-web/net/curl.jsonp";
		var params = {url:url};
		$.jsonp(service,{params:JSON.stringify(params)},function(json){
			if(json.success){
				alert('下载成功');
			}else{
				alert('下载失败');
			}
		});
	});
	$('.down-demo').off('click').click(function(){
		console.info('download now');
		var service = "//localhost:8080/demo1-web/net/curl-demo.jsonp";
		var url = location.href;
		var filename = location.pathname.replace(/\/$/,'');
		filename = filename.substr(filename.lastIndexOf('/'),filename.length);
		filename = filename.substr(0,filename.lastIndexOf('.'))||filename;
		filename +='/';
		var resourceroot = url.substring(0,url.indexOf(filename));
		var params = {url:url};
		$.jsonp(service,{params:JSON.stringify(params)},function(json){
			if(json.success){
				alert('下载成功');
			}else{
				alert('下载失败');
			}
		});
	});
	$('[resource]').off('click').click(function(e){
		console.info('download now');

		var service = "//localhost:8080/demo1-web/net/curl-demo-resource.jsonp";
		var page_resource_service = "//localhost:8080/demo1-web/net/curl-page-resource.jsonp";
		if( location.pathname.match(/\/js\/a\d+\.html/img)){
			service = page_resource_service;
		}
		var resource = $(this).attr('href');
		var url = location.href;
		var filename = location.pathname.replace(/\/$/,'');
		filename = filename.substr(filename.lastIndexOf('/')+1,filename.length);
		filename = filename.substr(0,filename.lastIndexOf('.'))||filename;
		var resourceroot = url.substring(0,url.indexOf(filename));
		var params = {url:url,resourceroot:resourceroot,resource:resource};

		$.jsonp(service,{params:JSON.stringify(params)},function(json){
			if(json.success){
				alert('下载成功');
			}else{
				alert('下载失败');
			}
		});
		e.preventDefault();
		e.returnValue = false;
		return false;
	});
	$('.down-page').off('click').click(function(){
		console.info('download now');
		var service = "//localhost:8080/demo1-web/net/curl-page.jsonp";
		var url = location.href;
		var filename = location.pathname.replace(/\/$/,'');
		filename = filename.substr(filename.lastIndexOf('/')+1,filename.length);
		filename = filename.substr(0,filename.lastIndexOf('.'))||filename;
		var resourceroot = url.substring(0,url.indexOf(filename));
		filename +='/';
		var params = {url:url,filename:filename,resourceroot:resourceroot};
		$.jsonp(service,{params:JSON.stringify(params)},function(json){
			if(json.success){
				alert('下载成功');
			}else{
				alert('下载失败');
			}
		});
	});
}
if(!window.jQuery){
	loader('script.src',['jquery-1.7.2.min.js'],'//localhost:7080/',{type:'text/javascript','charset':'UTF-8'},function(){
		jQuery.noConflict();
		loaded(window.jQuery);
	});	
}else{
	loaded(window.jQuery);
}